// pages/footprint/footprint.js
const app = getApp()
import {
        list
} from '../../../api/trace'

const util = require('../../../utils/util.js')

Page({

        /**
         * 页面的初始数据
         */
        data: {
                haveFootprint:true,
                pageNum:1,
                pageSize:20,
                createDateList:[], //日期的数组
                footprintList:[]
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
                this.getFootprintData()
        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function () {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function () {

        },

        //足迹数据初始化
        formateFootprintData(data){
                let footprintList = this.data.footprintList
                let createDateList = []
                let addFootprintList = []
                if (data.length > 0){
                        data.map((item)=>{
                                createDateList.push(item.createDate)
                        })

                        createDateList = Array.from(new Set(createDateList)) //去重
                        console.log("日期的数组createDateList--" + JSON.stringify(createDateList))
                        if (createDateList.length > 0) {
                                createDateList.map((item) => {
                                        let footprintListItem = {}
                                        footprintListItem.createDate = item
                                        footprintListItem.date = this.formatDate2day(item)
                                        footprintListItem.footprintProductList = []
                                        data.map((item1) => {
                                                if (item1.createDate == item){
                                                        let footprintProductListItem = {}
                                                        footprintProductListItem.id = item1.id
                                                        footprintProductListItem.name = item1.name
                                                        footprintProductListItem.thumbnail = util.thumbnail(item1.thumbnail, 215, 215)
                                                        footprintProductListItem.price = item1.price
                                                        footprintProductListItem.state = true
                                                        if (item1.stock == 0){
                                                                footprintProductListItem.tag = "缺货"
                                                                footprintProductListItem.state = false
                                                        }
                                                        if (item1.isMarketable == false) {
                                                                footprintProductListItem.tag = "下架"
                                                                footprintProductListItem.state = false
                                                        }
                                                        footprintListItem.footprintProductList.push(footprintProductListItem)
                                                }
                                        })
                                        addFootprintList.push(footprintListItem)
                                })
                                console.log("暂存补充的数据addFootprintList--" + JSON.stringify(addFootprintList))
                                if (addFootprintList.length > 0){
                                        addFootprintList.map((item)=>{
                                                let checkOff = false //检查是不是同一天的足迹
                                                footprintList.map((item1)=>{
                                                        if (item.createDate == item1.createDate){
                                                                checkOff =true 
                                                                item.footprintProductList.map(item2 => {
                                                                        item1.footprintProductList.push(item2)
                                                                }) 
                                                        }  
                                                })
                                                if (!checkOff){
                                                        footprintList.push(item)    
                                                }
                                        })
                                }
                        }
                        this.setData({
                                footprintList: footprintList 
                        })
                } 
        },

        //获取足迹信息
        getFootprintData(callback){
                console.log("请求用户的账户卡和福利金的余额数据--")
                let data = {
                        pageNum: this.data.pageNum,
                        pageSize: this.data.pageSize     
                }
                list(data).then(
                        res => {
                                console.log("接收用户信息--" + JSON.stringify(res))
                                if (res.type == 'success') {
                                        if (res.data.data.length > 0){
                                                this.formateFootprintData(res.data.data)
                                        }else{
                                                wx.showToast({
                                                        title: '没有更多足迹了',
                                                        icon:"none",
                                                        duration:2000
                                                })
                                        }
                                        if (callback) {
                                                callback(res.data.data)
                                        }
                                }
                        }
                ).catch(error => {
                        console.log(error)
                })
        },

        //上啦加载
        onReachBottom(e) {
                let _this = this
                console.info("下拉加载被触发了")
                let pageNum = this.data.pageNum
                pageNum++
                this.setData({
                        pageNum: pageNum
                })
                this.getFootprintData(()=>{
                        let scrollTop = _this.data.scrollTop - 50
                        wx.pageScrollTo({
                                scrollTop: scrollTop,
                                duration: 300
                        })
                })
                
        },

        selectItem: function (e) {
                let id = e.currentTarget.dataset.id
                if (e.currentTarget.dataset.state){
                        wx.navigateTo({
                                url: '/pages/goods/product/details?id=' + id
                        })
                }else{
                        console.info("无效商品商品被点击了")
                }
                
                
        },
        
        deleteItem: function (e) {
                console.info("删除商品被点击了")
                wx.showModal({
                        title: 'confirm的弹窗',
                        content: '确认要删除该项吗？',
                        success: function (res) {
                                if (res.confirm) {
                                        console.log('点击确认回调')
                                } else {
                                        console.log('点击取消回调')
                                }
                        }
                })
        },

        //时间格式化
        formatDate2day(val) {
                let date = new Date(val);
                let y = date.getFullYear();
                let MM = date.getMonth() + 1;
                MM = MM < 10 ? ('0' + MM) : MM;
                let d = date.getDate();
                d = d < 10 ? ('0' + d) : d;
                return y + '-' + MM + '-' + d;
        },

        //监听页面的滚动事件
        onPageScroll: function (e) {
                console.log(e.scrollTop);
                this.data.scrollTop = e.scrollTop
        },
})